//
//  LoginViewController.m
//  FoodList
//
//  Created by 王金晶 on 16/4/11.
//  Copyright © 2016年 王金晶. All rights reserved.
//

#import "LoginViewController.h"
#import "RegiestViewController.h"
#import "useModel.h"
@interface LoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *number;
@property (weak, nonatomic) IBOutlet UITextField *password;
@property(nonatomic,strong)NSString *iphone;
@property(nonatomic,strong)NSString *pass;
@property (weak, nonatomic) IBOutlet UIImageView *head;

@end

@implementation LoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"登录";
    
    UIBarButtonItem *left = [[UIBarButtonItem alloc]initWithTitle:@"返回" style:UIBarButtonItemStylePlain target:self action:@selector(left:)];
    self.navigationItem.leftBarButtonItem = left;
    
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc]initWithTitle:@"注册" style:UIBarButtonItemStylePlain target:self action:@selector(right:)];
    self.navigationItem.rightBarButtonItem = right;
    
    self.head.layer.cornerRadius = 50;
    self.head.layer.masksToBounds = YES;
}
- (void)left:(UIBarButtonItem *)left
{   if (_delegate1 && [_delegate1 respondsToSelector:@selector(changeRightViewController)] )
  {
      [self.delegate1 changeRightViewController];
  }
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark -------------回收键盘--------
- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event
{
    for (UITextField *textField in self.view.subviews)
    {
        [textField resignFirstResponder];
    }
}
- (void)right:(UIBarButtonItem *)right
{
    RegiestViewController *regiestVc = [[RegiestViewController alloc]initWithNibName:@"RegiestViewController" bundle:nil];
    [self.navigationController pushViewController:regiestVc animated:YES];
}

- (IBAction)login:(UIButton *)sender {
    
    _iphone = _number.text;
    _pass = _password.text;
    //转化成加密模式
    _pass = [NSString md5:_pass];
    NSLog(@"%@",_pass);
    [RequestManger requestWithURL:[NSString stringWithFormat:@"http://www.xdmeishi.com/index.php?m=mobile&c=index&a=login&account=%@&password=%@",_iphone,_pass] requestType:@"RequestGet" parDic:nil finish:^(NSData *data) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *dataDic = dic[@"data"];
        useModel *model = [[useModel alloc]init];
        [model setValuesForKeysWithDictionary:dataDic];
        [model setValue:dic[@"sessionId"] forKey:@"sessionId"];
        NSString *string = dic[@"description"];
        if ([dic[@"result"] isEqualToString:@"ok"]) {
            string = @"登录成功";
            //记录登录状态
            [[NSUserDefaults standardUserDefaults]setBool:YES forKey:@"loadState"];
            //昵称
            [[NSUserDefaults standardUserDefaults]setObject:model.nickname forKey:@"name"];
            [[NSUserDefaults standardUserDefaults]setObject:model.myID forKey:@"myid"];
            if (_delegate1 && [_delegate1 respondsToSelector:@selector(changetitle:)])
            {
                [self.delegate1 changetitle:model.nickname];
            }
            
        }
        UIAlertController *control = [UIAlertController alertControllerWithTitle:@"提示" message:string preferredStyle: UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            
        }];
        [control addAction:action];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSData *data = [[NSUserDefaults standardUserDefaults]objectForKey:@"image"];
            UIImage *image = [UIImage imageWithData:data];
            _head.image = image;
            [self presentViewController:control animated:YES completion:nil];
        });

    } error:nil];
}
- (IBAction)sinaLogin:(UIButton *)sender {
    UMSocialSnsPlatform *snsPlatform = [UMSocialSnsPlatformManager getSocialPlatformWithName:UMShareToSina];
    snsPlatform.loginClickHandler(self,[UMSocialControllerService defaultControllerService],YES,^(UMSocialResponseEntity *response){
        //          获取微博用户名、uid、token等
    if (response.responseCode == UMSResponseCodeSuccess) {
    UMSocialAccountEntity *snsAccount =[[UMSocialAccountManager socialAccountDictionary] valueForKey:UMShareToSina];
            NSLog(@"username is %@, uid is %@, token is %@ url is %@",snsAccount.userName,snsAccount.usid,snsAccount.accessToken,snsAccount.iconURL);
        [[NSUserDefaults standardUserDefaults]setObject:snsAccount.userName forKey:@"name"];
        if (_delegate1 && [_delegate1 respondsToSelector:@selector(changetitle:)])
        {
            [self.delegate1 changetitle:snsAccount.userName];
        }
        }});
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
